var ChangeOrderForm = function () {
  var template = /*html*/ `
    <div class="change-order-form" v-loading="loading">
      <el-form :model="machine" ref="changeOrderFormRef" class="changeOrderForm" label-width="85px">
        <el-form-item label="设备编号">
          <span>{{machine.equipmentNo}}</span>
        </el-form-item>
        <el-form-item label="设备名称">
          <span>{{machine.equipmentName}}</span>
        </el-form-item>
        <el-form-item label="原加工单号">
          <span>{{machine.orderNo}}</span>
        </el-form-item>
        <el-form-item label="加工单号">
          <order-select class="form-input" :options="orders" v-model="orderNo" valueField="orderId" 
            :selectOption.sync="changeOrder"></order-select>
        </el-form-item>
        <el-form-item label="调机数">
          <el-input-number class="form-input" v-model="adjustingMachineCount" :min="0" controls-position="right" 
            :precision="0"></el-input-number>
        </el-form-item>
      </el-form>
      <div style="text-align:center;margin-top:20px;">
        <el-button type="primary" @click="toUpdateOrder">切换</el-button>
      </div>
    </div>
  `;

  return {
    props: {
      machine: {
        type: Object,
        required: true
      }
    },
    data: function () {
      var adjustingMachineCount = this.machine.adjustingMachineCount;
      if (adjustingMachineCount === null || adjustingMachineCount === '') {
        adjustingMachineCount = undefined;
      }
      return {
        loading: false,
        orderNo: '',
        changeOrder: null,
        adjustingMachineCount: adjustingMachineCount,
        orders: []
      };
    },
    created: function () {
      this.setOrderList();
    },
    methods: {
      setOrderList: function () {
        var self = this;
        this.loading = true;
          ajaxRequest('weiXin/getOrderList').then(function (orders) {
          self.loading = false;
          self.orders = orders;
        }).catch(function () {
          self.loading = false;
        });
      },

      toUpdateOrder: function () {
        var machine = this.machine;
        var changeOrder = this.changeOrder;
        if (changeOrder === null) {
          this.$alert('请选择需要切换的加工订单', '信息');
          return;
        }

        var orderId = changeOrder.orderId;
        var postData = {
          equipmentNo: machine.equipmentNo,
          orderId: orderId,
          orderNo: this.orderNo
        };

        var adjustingMachineCount = this.adjustingMachineCount;
        if (adjustingMachineCount !== undefined){
          postData.adjustingMachineCount = adjustingMachineCount;
        }

        this.loading = true;
        var self = this;
        ajaxRequest('equipment/setEquipmentMachineOrder', postData).then(function () {
          self.loading = false;
          self.$emit('change-order', orderId);
        }).catch(function (e) {
          self.loading = false;
          if (e === undefined) {
            self.$emit('change-order-error');
          }
        });
      }
    },
    template: template,
    components: {
      OrderSelect: OrderSelect
    }
  };
}();